package com.projectplace.GUI.adapter;

/*
 * Copyright (c) 2011 Projectplace International AB
 * Permission is hereby granted, free of charge, to any person 
 * obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, 
 * publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:The above copyright notice and this 
 * permission notice shall be included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.io.File;
import java.util.HashMap;
import com.projectplace.R;
import com.projectplace.SingeltonPPClient;
import com.projectplace.api.UserApi;
import com.projectplace.api.objectResponses.ppApiCoWorkers;
import com.projectplace.api.objectResponses.ppApiUser;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Contacts_Adapter extends BaseAdapter{

    
	private final String LOG_TAG = "ImageAdapter";
    private Activity activity;
    private HashMap<String, Bitmap> storage;
    private ppApiCoWorkers coWorkers;
    private static LayoutInflater inflater = null;
	
    public Contacts_Adapter(Activity c,ppApiCoWorkers coWorkers){
    	activity = c;
    	this.coWorkers = coWorkers;
    	storage = new HashMap<String, Bitmap>();
		inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }
    
    public int getCount() {
        return coWorkers.size();
    }

    public ppApiUser getItem(int position) {
        return coWorkers.getCoWokerByIndex(position);
    }

    public long getItemId(int position) {
        return Long.parseLong(coWorkers.getCoWokerByIndex(position).getID());
    }
    
    
    
	public View getView(final int position, View convertView, final ViewGroup parent) {
    	final ViewHolder holder;	
    	final ppApiUser coworker = coWorkers.getCoWokerByIndex(position);
    	
    	if (convertView == null) {
 
    		convertView = inflater.inflate(R.layout.contactsgrid, parent,
					false);
    		holder = new ViewHolder();
    		holder.imageview = (ImageView)convertView.findViewById(R.id.contactPic);
    		holder.textview = (TextView)convertView.findViewById(R.id.contactName);
    		
    		convertView.setTag(holder);
        }else {
			holder = (ViewHolder) convertView.getTag();
		}
    	if (coworker != null) {
			holder.textview.setText(coworker.getFirst_Name() +" " +coworker.getLast_Name().charAt(0)+".");
			
				if(storage.containsKey(coworker.getID())){
					holder.imageview.setImageBitmap(storage.get(coworker.getID()));
			}
			else{ 
				Bitmap avatar = BitmapFactory.decodeFile(activity.getFilesDir()
						.toString()
						+ File.separatorChar
						+ coworker.getID()
						+ ".png");
				if (avatar != null){
					holder.imageview.setImageBitmap(avatar);
					storage.put(coworker.getID(), avatar);
				}else{
					UserApi.getAvatar(SingeltonPPClient.getInstance(),coworker.getID(), holder.imageview);
				}
			}
		}
        return convertView;
    }

    static class ViewHolder{
    	ImageView imageview;
    	TextView textview;
    }
}

